/**
 * The main page container ID
 */
var WHOLE_CONTAINER_ID = "bodyContent";

/**
 * The all content page ID
 */
var ALL_CONTENT_ID = "allContent";

/**
 * Sets the position to the element.
 *
 * @param offset
 *            A position
 * @param element
 *            An element
 * @return void
 */
function setPosition(offset, element) {

	element.css("top", offset.top + "px");
	element.css("left", offset.left + "px");
}

/**
 * Creates a generic info box for the element denoted by the passed ID.
 *
 * @param elementId
 *            An element ID
 * @param message
 *            The message to display
 * @return void
 */
function showInfoBoxById(elementId, message) {

	showInfoBox($("#" + elementId), message);
}

/**
 * Creates a generic info box for the passed element.
 *
 * @param element
 *            An element
 * @param message
 *            The message to display
 * @return void
 */
function showInfoBox(element, message) {

	// getting the info box
	var box = $("#infoBox");

	// creating a new box if none exists
	if (box.length == 0) {

		var htmlCnt = '<div id="infoBox"><p>' + message + '</p>';

		if (isIE6()) {

			htmlCnt += '<iframe frameborder="0" style="position:absolute;top:0;z-index:-10;width:100%;height:100%;border:0;"/>';
		}

		htmlCnt += '</div>';

		// creating the new box
		$("body").append(htmlCnt);

		if (isIE6()) {
			$("#infoBox iframe").fadeTo(0, 0);
		}

	} else {

		box.find("p").empty().append(message);
	}

	// getting the current box
	box = $("#infoBox");
	setPosition(element.offset(), box);

	// checking if the box should be left or right sided
	var container = $("#" + WHOLE_CONTAINER_ID);
	var containerRightLimit = container.offset().left + container.width();
	var boxRightLimit = element.offset().left + box.width();

	if (boxRightLimit > containerRightLimit) {

		box.addClass("leftSided");

	} else {

		box.removeClass("leftSided");
	}

	box.show();
}

/**
 * Hides the info box.
 *
 * @return void
 */
function hideInfoBox() {

	$("#infoBox").hide();
}

/**
 * Inits all info boxes on every table header found in the page
 *
 * @return void
 */
function initInfoBoxesOnTableHeader() {

	$("th.titleWithDetails").each(function() {

		var curEl = $(this);

		if (!curEl.hasClass("sortable")) {
			curEl.addClass("longTitle");
		}

		curEl.hover(function() {
			showInfoBox(curEl, curEl.find("span.longTitle").html());
		}, function() {
			hideInfoBox();
		});
	});
}

/**
 * Show an info box for the label denoted by the passed ID.
 *
 * @param labelId
 *            A label ID.
 * @param infoMessage
 *            The information message
 * @param otherManager
 *            Another message
 * @return void
 */
function showLabelInfoBox(labelId, infoMessage, otherMessage) {

	// getting the label info box
	var label = $("#" + labelId);
	var offset = label.offset();
	var box = $("#labelInfoBox");
	var newBoxContent = getLabelInfoxBoxContent(infoMessage, otherMessage);

	// creating a new box if none exists
	if (box.length == 0) {

		// creating the new box
		var htmlCnt = '<div id="labelInfoBox"><p>' + newBoxContent + '</p></div>';
		$("body").append(htmlCnt);

	} else {

		box.find("p").empty().append(newBoxContent);
	}

	// getting the current box and adding it to the label
	box = $("#labelInfoBox");

	if (isIE6() || isIE7()) {

		offset.left = offset.left + box.width();
		offset.top = offset.top - 5;
		setPosition(offset, box);

	} else {

		label.before(box);
	}

	box.show();
}

/**
 * Returns the HTML content to display in the label info box, according to the
 * passed messages.
 *
 * @param infoMessage
 *            The information message
 * @param otherManager
 *            Another message
 * @return HTML content
 */
function getLabelInfoxBoxContent(infoMessage, otherMessage) {

	var htmlCnt = '';

	if (infoMessage != null && infoMessage != '') {

		htmlCnt += infoMessage;

		if (otherMessage != null && otherMessage != '') {

			htmlCnt += '<br>';
		}
	}

	if (otherMessage != null && otherMessage != '') {

		htmlCnt += '<em>' + otherMessage + '</em>';
	}

	return htmlCnt;
}

/**
 * Hides the label info box.
 *
 * @return void
 */
function hideLabelInfoBox() {

	$("#labelInfoBox").hide();
}

/**
 * Displays the info box for the element for which an event was triggered.
 *
 * @param linkId
 *            A link ID.
 * @param titleMsg
 *            The message to display as a title
 * @param contentMsg
 *            The content message
 * @param leftSided
 *            Whether the box should be displayed at the left of the link.
 * @return void
 */
function showLinkInfoBox(linkId, titleMsg, contentMsg, leftSided) {

	var link = $("#" + linkId);
	var box = $("#linkInfoBox");
	var body = $("body");

	// creating a new box if none exists
	if (box.length == 0) {

		// creating the new box
		var htmlCnt = '<div id="linkInfoBox"><a class="closeBt" href="javascript:;">&nbsp;</a>' + '<p class="boxTitle"></p><p class="boxMsg"></p></div>';
		body.append(htmlCnt);
	}

	// getting the new box
	box = $("#linkInfoBox").unbind();

	// setting the messages
	box.find("p.boxTitle").empty().append(titleMsg);
	box.find("p.boxMsg").empty().append(contentMsg);

	var hideFn = function() {
		box.attr("class", "");

		if (leftSided) {

			box.addClass("leftSided");
		}

		box.slideUp().unbind();
	};

	if (box.hasClass(linkId)) {

		hideFn();

	} else {

		box.hide();
		box.attr("class", "").addClass(linkId);

		if (leftSided) {

			box.addClass("leftSided");
		}

		setPosition(link.offset(), box);
		box.slideDown();
		box.find("a.closeBt").unbind().click(hideFn);
	}
}

/**
 * Shows the popup box denoted by the passed ID.
 *
 * @param id
 *            A popup box ID
 * @return void
 */
function showPopupBox(id) {

	toggleVisible($("#" + id));
}

/**
 * Shows the popup box denoted by the passed ID under the element denoted by the
 * passed ID.
 *
 * @param refElId
 *            An element ID for which the box is displayed.
 * @param boxId
 *            The popup box ID
 * @return
 */
function showPopupList(refElId, boxId) {

	var box = $("#" + boxId);
	var el = $("#" + refElId);
	var offset = el.offset();
	offset.top = offset.top + el.height() + 5;
	toggleVisible(box);
	box.offset(offset);
}

/**
 * Shows the tickets box
 *
 * @param url
 *            The URL to retrieve the tickets box content
 * @return void
 */
function showTicketsBox(url) {

	var box = $("#tickets-cart");

	if (box.css("display") == "block") {

		setVisible(box, false);

	} else {

		box.load(url, function() {

			var isDown = checkIfSessionDown(box);

			if (!isDown) {

				showPopupList('tickets-cart-link', 'tickets-cart');
			}
		});
	}
}

/**
 *
 * @return The container hide zone. If none exists, create one.
 */
function getHideZone() {

	// getting/building the hide zone
	var hideZone = $("div.hideZone");

	if (hideZone.length == 0) {

		var hideZoneCnt = "";

		if (isIE6()) {

			hideZoneCnt = '<div class="hideZone"><iframe frameborder="0" style="width:100%;height:100%;"/></div>';

		} else {

			hideZoneCnt = '<div class="hideZone"></div>';
		}

		hideZone = $(hideZoneCnt).prependTo($("#" + WHOLE_CONTAINER_ID));
	}

	return hideZone;
}

/**
 * Shows the hide zone.
 *
 * @return void
 */
function showHideZone() {

	getHideZone().show();
}

/**
 * Hidess the hide zone.
 *
 * @return void
 */
function hideHideZone() {

	getHideZone().hide();
}

var SUB_PAGE_BOX_ID = "subPageBox";

/**
 * Inits a popup box with the content of the element denoted by the passed ID.
 *
 * @param title
 *            The title for the box
 * @param contentId
 *            The ID of the content element
 * @param btBarId
 *            The ID of the button bar element
 * @param closeBtFn
 *            A function to execute while closing the box
 * @param classAtt
 *            The class for the box
 * @return void
 */
function initSubPageBox(title, contentId, btBarId, closeBtFn, classAtt) {

	var container = $("body");

	// getting/building the box
	var box = $("#" + SUB_PAGE_BOX_ID);

	if (box.length == 0) {

		// creating the new box
		var htmlCnt = '<div id="' + SUB_PAGE_BOX_ID + '" class="' + classAtt
				+ '"><div class="boxCnt"><p class="boxTitle"></p>'
				+ '<div class="boxSubPageCnt"></div></div></div>';
		box = $(htmlCnt).appendTo(container);
	}

	// building the hide zone
	var hideZone = getHideZone();

	// setting the new title
	var boxTitle = box.find("div.boxCnt>p.boxTitle").empty().append(
			'<span>' + title + '</span>');

	// setting the new content
	var subPageCnt = $("#" + contentId);
	box.find("div.boxCnt>div.boxSubPageCnt").empty().append(subPageCnt);
	subPageCnt.show();

	// setting the button bar
	$('#' + btBarId).show().appendTo(box.find("div.boxCnt"));

	// setting the way to hide the box
	$('<span class="closeBt">&nbsp;</span>').prependTo(boxTitle).click(
			function() {
				box.hide();
				closeBtFn();
				hideHideZone();
			});

	// turning the box into a draggable one
	box.draggable( {
		disabled : false,
		appendTo : 'body',
		containment : [ 0, 0, $(window).height(), $(window).width() ],
		scroll : false,
		zIndex : 100000,
		handle : 'p.boxTitle',
		refreshPositions : false
	});
}

/**
 * Displays the sub page box.
 *
 * @return void
 */
function showSubPageBox() {

	var container = $("container");
	var box = $("#" + SUB_PAGE_BOX_ID);

	// adding the box to the container
	container.append(box);

	// positioning the box
	setPageWideBoxPosition(box);

	// showing the box
	box.show();

	// showing the hide zone
	showHideZone();
}

/**
 * Positions the passed box, that should be displayed on top of any element on
 * the page.
 *
 * @param box
 *            The box to display
 * @return void
 */
function setPageWideBoxPosition(box) {

	// computing the top and left coordinates for the box
	var win = $(window);
	var top = win.scrollTop() + (win.height() - box.height()) / 2;
	var left = win.scrollLeft() + (win.width() - box.width()) / 2;

	if (top < 0) {

		top = 0;
	}

	if (left < 0) {

		left = 0;
	}

	box.css("top", top + "px");
	box.css("left", left + "px");
}

/**
 * The current time out for the body info box
 */
var curBodyInfoBoxTimeOut;

/**
 * Displays an information box with the passed message on the screen, hiding all
 * other body elements.
 *
 * @param title
 *            The title for the box
 * @param message
 *            The message to display
 * @param type
 *            The type for the box : "info", "waiting", "confirm", "warning".
 * @param closingAllowed
 *            Whether the box can be closed.
 * @param closeAfterPeriod
 *            Whether the box should be automatically closed after 5 seconds.
 * @return void
 */
function showBodyBox(title, message, type, closingAllowed, closeAfterPeriod) {

	var boxId = "bodyInfoBox";
	var container = $("body");
	var box = $("#" + boxId);

	if (box.length == 0) {

		// creating the new box
		var htmlCnt = '<div id="' + boxId + '"><div class="boxCnt"><p class="boxTitle"></p><p class="boxMsg"></p></div></div>';
		box = $(htmlCnt).appendTo(container);
	}

	// building the hide zone
	var hideZone = getHideZone();

	// setting the box type
	box.removeAttr("class").addClass(type + "BodyBox");

	// setting the new title
	var boxTitle = box.find("div.boxCnt>p.boxTitle").empty().append(
			'<span>' + title + '</span>');

	// setting the new message
	box.find("div.boxCnt>p.boxMsg").empty().append(
			"<table><tr><td>" + message + "</td></tr></table>");

	// adding the box to the container
	container.append(box);

	// positioning the box
	setPageWideBoxPosition(box);

	// setting the way to hide the box
	var hideFn = function() {

		box.hide();
		hideHideZone();
	};

	// reinitializing the message box
	var boxCnt = box.find("div.boxCnt");
	boxCnt.removeClass("clickable");
	boxCnt.unbind();

	if (closingAllowed) {

		boxCnt.addClass("clickable");
		boxCnt.unbind().click(hideFn);
		boxTitle.prepend('<span class="closeBt">&nbsp;</span>');
	}

	if (closeAfterPeriod) {

		// setting a timer to close the box
		if (curBodyInfoBoxTimeOut != null) {

			clearTimeout(curBodyInfoBoxTimeOut);
		}

		curBodyInfoBoxTimeOut = setTimeout(hideFn, 2000);
	}

	// showing the hide zone
	showHideZone();

	// showing the box
	box.show();
}

/**
 * Displays a box used to ask the user for a confirmation, hiding all other body
 * elements.
 *
 * @param title
 *            The title for the box
 * @param message
 *            The message to display
 * @param type
 *            The type for the box : "confirm".
 * @param yesLabel
 *            The label for the "yes" button
 * @param noLabel
 *            The label for the "no" button
 * @param actionFn
 *            The function to execute when the "yes" button is clicked
 * @return void
 */
function showDialogBox(title, message, type, yesLabel, noLabel, actionFn) {

	var boxId = "dialogBox";
	var container = $("body");
	var box = $("#" + boxId);

	if (box.length == 0) {

		// creating the new box
		var htmlCnt = '<div id="' + boxId
				+ '"><div class="boxCnt"><p class="boxTitle"></p>'
				+ '<p class="boxMsg"></p><p class="buttonsCenterList">'
				+ '<button type="button" id="dialogBoxNoBt">'
				+ '<span class="before"></span><span class="content">'
				+ '<span class="btText">' + noLabel + '</span></span></button>'
				+ '<button type="button" id="dialogBoxYesBt">'
				+ '<span class="before"></span><span class="content">'
				+ '<span class="btText">' + yesLabel + '</span></span></button>'
				+ '</p></div></div>';
		container.append(htmlCnt);
	}

	box = $("#" + boxId);

	// building the hide zone
	var hideZone = getHideZone();

	// setting the box type
	box.removeAttr("class").addClass(type + "DialogBox");

	// setting the new title
	var boxTitle = box.find("div.boxCnt>p.boxTitle").empty().append(
			'<span>' + title + '</span>');

	// setting the new message
	box.find("div.boxCnt>p.boxMsg").empty().append(
			"<table><tr><td>" + message + "</td></tr></table>");

	// getting the buttons
	var noBt = box.find("#dialogBoxNoBt").unbind().click(function() {
		box.hide();
		hideHideZone();
	});

	box.find("#dialogBoxYesBt").unbind().click(function() {
		actionFn();
	});

	// adding the box to the container
	container.append(box);

	// positioning the box
	setPageWideBoxPosition(box);

	// showing the box
	box.show();
	noBt.focus();

	// showing the hide zone
	showHideZone();
}